<template>
  <!-- 采购计划关联采购单明细 -->
  <a-table class="table-normal-line show-border-table" ref="tableListref" :span-method="spanMethodAll" :summary="true" row-key="id" :expandable="expandable" :default-expand-all-rows="true" :loading="tableLoading" :columns="tableColumns" :data="tableList" size="mini" :pagination="false" :scroll="{ x: 1200, y: '100%' }">
    <template #columns>
      <a-table-column v-for="(item, index) of tableColumns" :fixed="item.fixed" :key="index" :align="item.align" :width="item.width" :title="item.title" :data-index="item.dataIndex">
        <template v-if="item.dataIndex == 'deliveryAmt'" #title>
          <a-popover content="交货单创建是填写的交货数">
            <p>
              <span>交货数</span>
              <icon-question-circle />
            </p>
          </a-popover>
        </template>
        <template v-else-if="item.dataIndex == 'deliveryRealAmt'" #title>
          <a-popover content="确认交货单时实际收到的数量">
            <p>
              <span>实际交货数</span>
              <icon-question-circle />
            </p>
          </a-popover>
        </template>
        <template v-else-if="item.dataIndex == 'deliveryDiffAmt'" #title>
          <a-popover content="交货差额=交货数量-实际交货数量">
            <p>
              <span>交货差额</span>
              <icon-question-circle />
            </p>
          </a-popover>
        </template>
        <template v-else-if="item.dataIndex == 'warehouseRealAmt'" #title>
          <a-popover content="交货单实际入库的数量">
            <p>
              <span>已入库数</span>
              <icon-question-circle />
            </p>
          </a-popover>
        </template>
        <template v-else-if="item.dataIndex == 'warehouseReturnAmt'" #title>
          <a-popover content="仓库签收入库时会再次清点检查数量，这里的退回数=实际交货数-实际入库数">
            <p>
              <span>仓库退回数</span>
              <icon-question-circle />
            </p>
          </a-popover>
        </template>
        <template v-else-if="item.dataIndex == 'deliveryValue'" #title>
          <a-popover content="已交货金额=单价*实际交货数">
            <p>
              <span>已交货金额</span>
              <icon-question-circle />
            </p>
          </a-popover>
        </template>
        <template v-else-if="item.dataIndex == 'unitCosts'" #title>
          <a-popover content="单件成本=单价+单件工费">
            <p>
              <span>单件成本</span>
              <icon-question-circle />
            </p>
          </a-popover>
        </template>
        <template v-else-if="item.dataIndex == 'inventoryCosts'" #title>
          <a-popover content="入库货值=单件成本*实际入库数">
            <p>
              <span>入库货值</span>
              <icon-question-circle />
            </p>
          </a-popover>
        </template>
        <template v-if="item.dataIndex == 'img'" #cell="{ record }">
          <div class="order-w flex">
            <div class="order-detail-item" style="width: 160px">
              <p>
                <span class="label">交货单号：</span>
                <span class="highlight cursor">{{ record.billNo }}</span>
              </p>
            </div>
            <div class="order-detail-item flex-c" style="width: 250px">
              <span class="label">目的仓库：</span>
              <TextLimit style="flex: 1" :val="`${record.warehouseName}(${record.warehouseCode})`" :rows="1" />
            </div>
            <div class="order-detail-item flex-c" style="width: 250px">
              <span class="label">供应商：</span>
              <TextLimit style="flex: 1; color: #666" :val="`${record.supplierName}(${record.supplierCode})`" :rows="1" />
            </div>
            <div class="order-detail-item" style="width: 200px">
              <p><span class="label">交货日期：</span>{{ $setTime(record.gmtDelivery, 'YYYY/MM/DD HH:mm') }}</p>
            </div>
          </div>
        </template>
      </a-table-column>
    </template>
    <template #expand-row="{ record }">
      <a-table class="table-singe-line show-border-table" row-class="child-row" :show-header="false" row-key="id" :bordered="false" :columns="tableColumns" :data="record.billItems" size="mini" :pagination="false">
        <template #columns>
          <a-table-column :width="10" />
          <a-table-column v-for="(item, index) of tableColumns" :key="index" :fixed="item.fixed" :align="item.align" :width="item.width" :title="item.title" :data-index="item.dataIndex">
            <template v-if="item.dataIndex == 'shop'" #cell="option">
              <TextLimit>{{ option.record.shopName }}</TextLimit>
            </template>
            <template v-else-if="item.dataIndex == 'img'" #cell="option">
              <ImgPopover :src="option.record.productImg">
                <template #con>
                  <a-image width="40" height="40" fit="contain" :src="option.record.productImg" />
                </template>
              </ImgPopover>
            </template>
            <template v-else-if="item.dataIndex == 'productName'" #cell="option">
              <TextLimit :rows="1" :val="option.record.productName" />
            </template>
            <template v-else-if="item.dataIndex == 'sku'" #cell="option">
              <ShowSpuDetailBySku :val="option.record.sku" :showLimit="true" :rows="1" />
              <p>{{ option.record.cateName || '--' }}</p>
            </template>
            <template v-else-if="item.dataIndex == 'productSpecs'" #cell="option">
              <TextLimit :val="getProductSpecs(option.record)" />
            </template>
            <template v-else-if="item.dataIndex == 'rate'" #cell="option">
              <p v-if="option.record">{{ option.record.rate }}%</p>
            </template>
            <template v-else-if="item.dataIndex == 'price'" #cell="option">
              <p class="price-type highlight">{{ option.record.priceType === 1 ? '含税' : '不含税' }}</p>
              <p>￥{{ option.record.price }}</p>
            </template>
            <template v-else-if="item.dataIndex == 'deliveryValue'" #cell="option">
              <p class="price-type highlight">{{ option.record.priceType === 1 ? '含税' : '不含税' }}</p>
              <p>￥{{ option.record.deliveryValue }}</p>
            </template>
            <template v-else-if="item.dataIndex == 'singleLaborCosts'" #cell="option">
              <p>￥{{ option.record.singleLaborCosts }}</p>
            </template>
            <template v-else-if="item.dataIndex == 'unitCosts'" #cell="option">
              <p>￥{{ option.record.unitCosts }}</p>
            </template>

            <template v-else-if="item.dataIndex == 'inventoryCosts'" #cell="option">
              <p>￥{{ option.record.inventoryCosts }}</p>
            </template>
          </a-table-column>
        </template>
      </a-table>
    </template>
    <template #summary-cell="{ column, record }">
      <p v-if="column.dataIndex == 'sku'">合计</p>
      <p v-else-if="['deliveryAmt', 'deliveryRealAmt', 'warehouseRealAmt'].includes(column.dataIndex)">{{ record[column.dataIndex] }}</p>
      <p v-else-if="['inventoryCosts'].includes(column.dataIndex)">￥{{ record[column.dataIndex] }}</p>
    </template>
  </a-table>
</template>
<script lang="ts" setup>
  import { ref, onMounted } from 'vue'
  import { findDeliveryItemByPlan, findDeliveryByPurchase } from '@/api/purchase'
  import { parseJson } from '@/utils'

  const props = defineProps({
    currentData: {
      type: Object,
      default: () => {},
    },
    type: {
      type: String,
      default: 'purchasePlan',
    },
  })
  onMounted(() => {
    getTableList()
  })

  const tableList = ref<Array<any>>([])
  const tableLoading = ref(false)
  const expandable = ref<any>({
    title: '',
    defaultExpandAllRows: true,
    expandedRowKeys: [],
    width: 10,
  })
  const tableColumns: Array<any> = [
    {
      title: '图片',
      dataIndex: 'img',
      width: 60,
    },
    {
      title: '产品名称',
      dataIndex: 'productName',
      width: 120,
    },
    {
      title: 'SKU/类目',
      dataIndex: 'sku',
      width: 100,
    },
    {
      title: '规格名',
      dataIndex: 'productSpecs',
      width: 100,
    },
    {
      title: '交货数',
      dataIndex: 'deliveryAmt',
      width: 80,
    },
    {
      title: '实际交货数',
      dataIndex: 'deliveryRealAmt',
      width: 90,
    },
    {
      title: '交货差额',
      dataIndex: 'deliveryDiffAmt',
      width: 80,
    },
    {
      title: '已入库数',
      dataIndex: 'warehouseRealAmt',
      width: 80,
    },
    {
      title: '仓库退回数',
      dataIndex: 'warehouseReturnAmt',
      width: 90,
    },
    {
      title: '税率',
      dataIndex: 'rate',
      width: 70,
    },
    {
      title: '单价',
      dataIndex: 'price',
      width: 70,
    },
    {
      title: '已交货金额',
      dataIndex: 'deliveryValue',
      width: 95,
    },
    {
      title: '单件工费',
      dataIndex: 'singleLaborCosts',
      width: 70,
    },
    {
      title: '单件成本',
      dataIndex: 'unitCosts',
      width: 85,
    },
    {
      title: '入库货值',
      dataIndex: 'inventoryCosts',
      width: 90,
    },
  ]
  async function getTableList() {
    tableLoading.value = true
    if (!props.currentData.id) return
    let res: any
    if (props.type == 'purchasePlan') {
      res = await findDeliveryItemByPlan({
        id: props.currentData.id,
      })
    } else if (props.type == 'purchaseOrder') {
      res = await findDeliveryByPurchase({
        id: props.currentData.id,
      })
    }
    tableLoading.value = false
    tableList.value = res?.data || []
    expandable.value.expandedRowKeys = tableList.value.map((el: any) => el.id)
  }
  // 合并单元格
  function spanMethodAll(option: any) {
    let { column } = option
    if (column.dataIndex === 'img') {
      return { rowspan: 1, colspan: 15 }
    }
  }
  // 显示sku规格
  function getProductSpecs(item: any) {
    let productSpecs = item.productSpecs ? parseJson(item.productSpecs, []) : []
    return productSpecs.map((el: any) => el.valueCn)?.join('*') || '--'
  }
</script>
<style lang="less" scoped>
  :deep(.arco-table-td) {
    background-color: var(--color-fill-1);
    height: auto;
  }
  :deep(.arco-table-tr-expand) {
    .arco-table-cell {
      padding: 0;
    }
    .arco-table {
      margin: 0 !important;
      .arco-table-td {
        border-bottom: 1px solid var(--color-neutral-3);
        height: 48px;
      }
    }
    background-color: #fff !important;
  }
  :deep(.child-row) {
    .arco-table-cell {
      padding: 2px 8px;
    }
  }
  :deep(.arco-table-tr-expand .arco-table-td) {
    background-color: #fff !important;
    border-bottom: none;
  }
  :deep(.arco-table-tr-expand tr:hover .arco-table-td) {
    background-color: var(--color-fill-1) !important;
  }
  :deep(.arco-table-expand .arco-table-cell) {
    display: none;
  }
  .order-w {
    color: #666;
    flex: 1;
    .order-detail-item {
      width: 200px;
      padding-right: 5px;
    }
  }
  .price-type {
    font-size: 12px;
    transform: scale(0.82);
    position: relative;
    left: -4px;
  }
</style>
